package com.qfedu;

import com.alibaba.fastjson.JSON;
import com.qfedu.domain.Clazz;
import com.qfedu.domain.Student;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建者: QF-SUN
 * 创建时间: 2023/3/30 11:36
 * 描述:
 */
public class TestJSON {

    public static void main(String[] args) {
        //创建一个学生对象
//        Student stu = new Student(1, "李志", 18, "中国北科");

        List<Student> list = new ArrayList<>();
        Clazz clazz = new Clazz(1,"2301");
        list.add(new Student(1, "李志", 18, "中国北科", clazz));
        list.add(new Student(2, "曹雷", 18, "中国北科", clazz));
        list.add(new Student(3, "郑家书", 18, "中国北科", clazz));
        list.add(new Student(4, "龚博宇", 18, "中国北科", clazz));
        //把对象转化为JSON串
//        String json = JSON.toJSONString(stu);
//        System.out.println(json);
//
        String jsonList = JSON.toJSONString(list);
        System.out.println(jsonList);
//        String jsonStr = "{\"address\":\"中国北科\",\"age\":18,\"id\":1,\"name\":\"李志\"}";
        //把串转换为对象
//        Student stu = JSON.parseObject(jsonStr, Student.class);
//        System.out.println(stu);

//        String jsonArray = "[{\"address\":\"中国北科\",\"age\":18,\"id\":1,\"name\":\"李志\"},{\"address\":\"中国北科\",\"age\":18,\"id\":2,\"name\":\"曹雷\"},{\"address\":\"中国北科\",\"age\":18,\"id\":3,\"name\":\"郑家书\"},{\"address\":\"中国北科\",\"age\":18,\"id\":4,\"name\":\"龚博宇\"}]";
        List<Student> stuList = JSON.parseArray(jsonList, Student.class);
        System.out.println(stuList);

    }

}
